#include <iostream>
using namespace std;
const int N = 1010, M = 110;
int prices[N];
int n, k;
int f[N][M], g[N][M];
int ans = 0;
int main() {
    cin >> n >> k;
    for (int i = 0; i < n; i++) cin >> prices[i];
    k = min(k, n / 2);
    for (int j = 0; j <= k; j++) f[0][j] = g[0][j] = -0x3f3f3f3f;
    f[0][0] = -prices[0];
    g[0][0] = 0;
    for (int i = 1; i < n; i++) {
        for (int j = 0; j <= k; j++) {
            f[i][j] = max(f[i - 1][j], g[i - 1][j] - prices[i]);
            g[i][j] = g[i - 1][j];
            if (j > 0) g[i][j] = max(g[i][j], f[i - 1][j - 1] + prices[i]);
        }
    }
    for (int i = 0; i <= k; i++) ans = max(ans, g[n - 1][i]);
    cout << ans << endl;
    return 0;
}